//
//  HomeView.m
//  YTAutoLayout
//
//  Created by fly on 14/12/12.
//  Copyright (c) 2014年 fly. All rights reserved.
//

#import "HomeView.h"

@interface HomeView ()<UIScrollViewDelegate>

@property(nonatomic ,weak) UIPageControl* pageControl;

@end

@implementation HomeView

-(instancetype)initWithPicCount:(int) count{
    if(self =  [super init])
    {
        [self createHomeView:count];
    }
    return self;
}

-(void)createHomeView:(int) count{
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    
     [self addSubview:[[UIView alloc] initWithFrame:self.frame]];
    //一、创建子视图
    
    //1 创建UIscrollview
    UIScrollView *scrollView= [[UIScrollView alloc] init];
    
    for (int i =0; i<count;i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"zhuye%d",i+1]]];
        
        imageView.frame = CGRectMake(i*screenSize.width, 0, screenSize.width, screenSize.height);
        
        [scrollView addSubview:imageView];
    }
    scrollView.contentSize =CGSizeMake(count*screenSize.width,screenSize.height);
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    //1.1 设置scrollview的translatesAutoresizingMaskIntoConstraints属性为NO
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    //scrollView.contentOffset= CGPointMake(screenSize.width, 0);
    
    //2.创建UIView
    UIView *buttomView  = [[UIView alloc] init];
    //buttomView.backgroundColor = [UIColor whiteColor];
    
    //2.1设置UIView的translatesAutoresizingMaskIntoConstraints属性为NO
    buttomView.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    //3.创建登录 设置按钮Btn
    UIButton *loginBtn = [[UIButton alloc] init];
    [loginBtn setTitle:@"" forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"login_icon"] forState:UIControlStateNormal];
    //loginBtn.backgroundColor  =[UIColor blueColor];
    loginBtn.translatesAutoresizingMaskIntoConstraints  = NO;

    UIButton *setingBtn = [[UIButton alloc] init];
    [setingBtn setTitle:@"" forState:UIControlStateNormal];
    [setingBtn setBackgroundImage:[UIImage imageNamed:@"option_icon"] forState:UIControlStateNormal];
    setingBtn.translatesAutoresizingMaskIntoConstraints  = NO;
    
    //创建pagecontroller
    UIPageControl *pageControl =  [[UIPageControl alloc] init];
    _pageControl = pageControl;
    pageControl.translatesAutoresizingMaskIntoConstraints = NO;
    pageControl.numberOfPages = count;
    pageControl.currentPage = 0;
    //3.将LoginBtn添加到buttomViewView中
    [buttomView addSubview:loginBtn];
    
    
    //二、添加子视图到父视图
    [self addSubview:scrollView];
    [self addSubview:buttomView];
    [self addSubview:pageControl];
    [self addSubview:setingBtn];
    //三、添加约束
    //1.scrollview的约束
    
    NSMutableArray *scrollConstraints = [[NSMutableArray alloc] init];
    
    //添加scrollView的水平约束
    [scrollConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[scrollView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
    
    //添加scrollView的垂直约束
    [scrollConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
    
    [self addConstraints:scrollConstraints];
    scrollConstraints = nil;
    
    //2.buttomView的约束
    NSMutableArray *bottomViewConstraints = [[NSMutableArray alloc] init];
    
    //添加scrollView的水平约束
    [bottomViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[buttomView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(buttomView)]];
    
    //添加scrollView的垂直约束
    [bottomViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[buttomView(==100)]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(buttomView)]];
    
    [self addConstraints:bottomViewConstraints];
    
    bottomViewConstraints = nil;
    
    //3添加buttons的约束
    NSMutableArray *loginBtnConstraints = [[NSMutableArray alloc] init];
    
    //BtnLogin的约束条件
    [loginBtnConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[loginBtn]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(loginBtn,setingBtn)]];
    
    [loginBtnConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[loginBtn]-30-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(loginBtn)]];
    
  

    [buttomView addConstraints:loginBtnConstraints];
    loginBtnConstraints = nil;
    
    //pageControl添加约束
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    [array addObject:[NSLayoutConstraint constraintWithItem:pageControl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    
    [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[pageControl]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pageControl)]];
    
    
    //设置settingBtn的约束条件
    
    [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[setingBtn(==30)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(setingBtn)]];
    
    [array addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[setingBtn(==30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(setingBtn)]];
    
    
    [self addConstraints:array];

    
    array = nil;
    
    //设置按钮监听事件  并通过代理进行传递
    [loginBtn addTarget:self action:@selector(loginClicked:forEvent:) forControlEvents:UIControlEventTouchDown];
    [setingBtn addTarget:self action:@selector(settingClicked:forEvent:) forControlEvents:UIControlEventTouchDown];
    
}

- (void)loginClicked:(id)sender forEvent:(UIEvent *)event{
    //NSLog(@"login");
    [self.delegate homeViewLogin];
}

- (void)settingClicked:(id)sender forEvent:(UIEvent *)event{
    [self.delegate homeViewSetting];
}

#pragma mark 设置scrollview的代理

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //NSLog(@"scrolling");
   int page= (scrollView.contentOffset.x+([[UIScreen mainScreen]bounds].size.width/2))/[[UIScreen mainScreen]bounds].size.width;
    //NSLog(@"%d",page);
    _pageControl.currentPage = page;
}

@end
